package com.idbk.chargestation.activity.user;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.MywalletAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonMywalletRecord;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;

import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * 查看记录明细
 * @author zhx
 *
 */
public class ActivityRecordDetail extends BaseActivity {
	
	private int tradingLogId,detailType;
	private TextView mDetailType, mDetailTime, mMoney, mMoneyValue, mPoint, mPointNum;
	private TextView mBookSn, mBookSnNum, mChargeTime, mChargeTimeValue, mElectricity, mElectricityNum;
	private TextView mOrderTime, mTime, mTotalCost, mTotalCostNum;
	private LinearLayout mChargeStopTime;
	private TextView mTexChargeStopText, mTexChargeStopTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_record_detail);

		setupView();

		setupData();
	}

	private void setupView() {
		setupToolBar2();//显示标题栏
		tradingLogId = getIntent().getExtras().getInt(MywalletAdapter.TRADING_LOG_ID);
		detailType = getIntent().getExtras().getInt(MywalletAdapter.DETAIL_TYPE);

		mDetailType = (TextView)findViewById(R.id.detail_type);
		mDetailTime = (TextView)findViewById(R.id.detail_time);
		mMoney = (TextView)findViewById(R.id.money);
		mMoneyValue = (TextView)findViewById(R.id.textview_money);
		mPoint = (TextView)findViewById(R.id.point);
		mPointNum = (TextView)findViewById(R.id.textview_point);

		mBookSn = (TextView)findViewById(R.id.booksn);
		mBookSnNum = (TextView)findViewById(R.id.textview_booksn);
		mChargeTime = (TextView)findViewById(R.id.chargetime);
		mChargeTimeValue = (TextView)findViewById(R.id.textview_chargetime);
		mElectricity = (TextView)findViewById(R.id.electricity);
		mElectricityNum = (TextView)findViewById(R.id.textview_electricity);

		mOrderTime = (TextView)findViewById(R.id.order_time);
		mTime = (TextView)findViewById(R.id.textview_time);
		mTotalCost = (TextView)findViewById(R.id.totalcost);
		mTotalCostNum = (TextView)findViewById(R.id.textview_totalcost);

		mChargeStopTime = (LinearLayout)findViewById(R.id.charge_stop_time);
		mChargeStopTime.setVisibility(View.GONE);
		mTexChargeStopText = (TextView)findViewById(R.id.stop_time);
		mTexChargeStopTime = (TextView)findViewById(R.id.textview_stop_charge_time);
	}

	private void setupData() {
		switch (detailType) {
		case JsonMywalletRecord.CHARGE_CONSUMPTION:
			mDetailType.setText("充电消费");
			ChargeStationAPI.getConsumptiontRecord(tradingLogId, JsonMywalletRecord.CHARGE_CONSUMPTION, chargeResponse);
			break;

		case JsonMywalletRecord.PREPARED_CONSUMPTION:
			mDetailType.setText("预约消费");
			ChargeStationAPI.getConsumptiontRecord(tradingLogId, JsonMywalletRecord.PREPARED_CONSUMPTION, orderResponse);
			break;

		case JsonMywalletRecord.ALIPAY_RECHARGE:
		case JsonMywalletRecord.WECHAT_RECHARGE:
			mDetailType.setText("充值");
			ChargeStationAPI.getRechargeRecord(tradingLogId, rechargeResponse);
			break;

		case JsonMywalletRecord.BACKGROUND_PRESENTED:
		case JsonMywalletRecord.RECHARGE_PRESENTED:
		case JsonMywalletRecord.INTRODUCTION_PRESENTED:
		case JsonMywalletRecord.REGISTRATION_PRESENTED:
			mDetailType.setText("赠送");
			ChargeStationAPI.getGiftRecord(tradingLogId, giftResponse);
			break;
		case JsonMywalletRecord.PARKING_CHARGE:
			mDetailType.setText("停车消费");
			ChargeStationAPI.getConsumptiontRecord(tradingLogId, JsonMywalletRecord.PARKING_CHARGE, parkResponse);
			break;
		case JsonMywalletRecord.REFUND:
			mDetailType.setText("退款");
			ChargeStationAPI.getRechargeRecord(tradingLogId, rechargeResponse);
			break;
		default:
			break;
		}
	}

	private final EHttpResponse chargeResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecord result = GsonUtils.toBean(JsonMywalletRecord.class, response);
			if (handleResponseStatus(ActivityRecordDetail.this, result)) {
				mDetailTime.setText(result.recordTime);
				mMoney.setText("充电费用 :  ￥");
				mMoneyValue.setText(String .format(Locale.CHINA, "%.2f",result.money));
				mPointNum.setText(result.pointName);
				mBookSn.setText("充电电桩:");
				String sn = "#"+result.pileSn;
				mBookSnNum.setText(sn);

				mChargeTime.setText("充电电量:");
				String electricity = result.sumQuantity + " 度";
				mChargeTimeValue.setText(electricity);

				mElectricity.setText("开始充电时间:");
				mElectricityNum.setText(result.getStartChargeTime());

				mChargeStopTime.setVisibility(View.VISIBLE);
				mTexChargeStopText.setText("结束充电时间:");
				mTexChargeStopTime.setText(result.getStopChargeTime());

				mOrderTime.setText("服务费用:");
				String order = result.serviceMoney + " 元";
				mTime.setText(order);
				mTotalCost.setText("电费:");
				if (result.chargeMoneys == null) {
					String money = "0.00 元";
					mTotalCostNum.setText(money);
				} else {
					String money = result.totalChargeMoney() + " 元";
					mTotalCostNum.setText(money);
				}
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(ActivityRecordDetail.this, R.string.common_tip_resultfail, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
		}
	};

	private final EHttpResponse orderResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecord result = GsonUtils.toBean(JsonMywalletRecord.class, response);
			if (handleResponseStatus(ActivityRecordDetail.this, result)) {
				mDetailTime.setText(result.recordTime);
				mMoney.setText("预约费用 :  ￥");
				mMoneyValue.setText(String .format(Locale.CHINA, "%.2f",result.money));
				mPointNum.setText(result.pointName);

				mBookSn.setText("预约电桩:");
				String sn = "#" + result.pileSn;
				mBookSnNum.setText(sn);
				mChargeTime.setText("预约时间:");
				SimpleDateFormat format = new SimpleDateFormat("HH:mm",Locale.CHINA);
				String start = format.format(result.startBookTime);
				String stop = format.format(result.stopBookTime);
				mChargeTimeValue.setText(start +" - "+stop);
				mElectricity.setVisibility(View.GONE);
				mElectricityNum.setVisibility(View.GONE);
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(ActivityRecordDetail.this,
					R.string.common_tip_resultfail,
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
		}
	};

	private final EHttpResponse rechargeResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecord result = GsonUtils.toBean(JsonMywalletRecord.class, response);
			if (handleResponseStatus(ActivityRecordDetail.this, result)) {
				mDetailTime.setText(result.recordTime);

				mBookSn.setText("交易状态:");
				if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_FINISH){
					mBookSnNum.setText("交易完成");

				}else if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_WAITING){
					mBookSnNum.setText("等待支付");

				}else if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_CANCEL){
					mBookSnNum.setText("取消支付");

				}else if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_UNAVAILABLE){
					mBookSnNum.setText("订单失效");

				}else {
					mBookSnNum.setText("数据出错");
				}
				mChargeTime.setText("交易流水号:");
				mChargeTimeValue.setText(result.tradingNo);
				mElectricity.setVisibility(View.GONE);
				mElectricityNum.setVisibility(View.GONE);

				if (detailType == JsonMywalletRecord.REFUND) {
					mMoney.setText("退款金额 :  ￥");
					mMoneyValue.setText(String .format(Locale.CHINA, "%.2f",result.money));
					mPoint.setText("交易类型:");
					mPointNum.setText("退款");
					mOrderTime.setVisibility(View.GONE);
					mTime.setVisibility(View.GONE);

					mTotalCost.setVisibility(View.GONE);
					mTotalCostNum.setVisibility(View.GONE);

				} else {
					mMoney.setText("充值金额 :  ￥");
					mMoneyValue.setText(String .format(Locale.CHINA, "%.2f",result.money));
					mPoint.setText("交易类型:");
					if(detailType == JsonMywalletRecord.ALIPAY_RECHARGE){
						mPointNum.setText("支付宝充值");
					} else {
						mPointNum.setText("微信充值");
					}
					mOrderTime.setText("充值金额: ");
					String money = String .format(Locale.CHINA, "%.2f",result.rechargeMoney) + " 元";
					mTime.setText(money);

					mTotalCost.setText("赠送金额: ");
					String cost = String .format(Locale.CHINA, "%.2f",result.giftMoney) + " 元";
					mTotalCostNum.setText(cost);
				}
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(ActivityRecordDetail.this,
					R.string.common_tip_resultfail,
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
		}
	};


	private final EHttpResponse giftResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecord result = GsonUtils.toBean(JsonMywalletRecord.class, response);
			if (handleResponseStatus(ActivityRecordDetail.this, result)) {
				mDetailTime.setText(result.recordTime);

				mMoney.setText("充值金额 :  ￥");
				mMoneyValue.setText(String .format(Locale.CHINA, "%.2f",result.money));
				mPoint.setText("交易类型:");
				if(detailType == JsonMywalletRecord.BACKGROUND_PRESENTED){
					mPointNum.setText("后台赠送");

				}else if(detailType == JsonMywalletRecord.RECHARGE_PRESENTED){
					mPointNum.setText("充值赠送");

				}else if(detailType == JsonMywalletRecord.INTRODUCTION_PRESENTED){
					mPointNum.setText("介绍赠送");

				}else if(detailType == JsonMywalletRecord.REGISTRATION_PRESENTED){
					mPointNum.setText("注册赠送");

				}else {
					mPointNum.setText("数据出错");
				}

				mBookSn.setText("交易状态:");
				if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_FINISH){
					mBookSnNum.setText("交易完成");

				}else if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_WAITING){
					mBookSnNum.setText("等待支付");

				}else if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_CANCEL){
					mBookSnNum.setText("取消支付");

				}else if(result.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_UNAVAILABLE){
					mBookSnNum.setText("订单失效");

				}else {
					mBookSnNum.setText("数据出错");
				}

				mChargeTime.setText("交易流水号:");
				mChargeTimeValue.setText(result.tradingNo);
				mElectricity.setVisibility(View.GONE);
				mElectricityNum.setVisibility(View.GONE);


				mOrderTime.setText("充值金额: ");
				String time = String .format(Locale.CHINA, "%.2f",result.rechargeMoney) + " 元";
				mTime.setText(time);

				mTotalCost.setText("赠送金额: ");
				String cost = String .format(Locale.CHINA, "%.2f",result.giftMoney) + " 元";
				mTotalCostNum.setText(cost);
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(ActivityRecordDetail.this,
					R.string.common_tip_resultfail,
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
		}
	};

	private final EHttpResponse parkResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecord result = GsonUtils.toBean(JsonMywalletRecord.class, response);
			if (handleResponseStatus(ActivityRecordDetail.this, result)) {
				mDetailTime.setText(result.recordTime);
				mMoney.setText("停车费用 :  ￥");
				mMoneyValue.setText(String .format(Locale.CHINA, "%.2f",result.money));
				mPointNum.setText(result.pointName);

				mBookSn.setText("充电电桩:");
				String sn = "#" + result.pileSn;
				mBookSnNum.setText(sn);
				mChargeTime.setText("停车时长:");
				SimpleDateFormat format = new SimpleDateFormat("HH:mm",Locale.CHINA);
				String start=format.format(result.startParkingTime);
				String stop=format.format(result.stopParkingTime);
				mChargeTimeValue.setText(start +" - "+stop);
				mElectricity.setVisibility(View.GONE);
				mElectricityNum.setVisibility(View.GONE);
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(ActivityRecordDetail.this,
					R.string.common_tip_resultfail,
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
		}	
	};
	
}
